package ntu.metaweb.update;

import java.io.IOException;
import java.net.URL;

import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.OutputBuffer;
import ntu.metaweb.XMLMarshaller;
import ntu.p2p.dht.messaging.DHTObjectDifference;

public class DeleteLink implements DHTObjectDifference {
	protected static final short TYPE = 11;
	
	public URL from;
	public URL to;
	public String user;
	
	public DeleteLink(URL from, URL to, String user) {
		this.from = from;
		this.to = to;
		this.user = user;
	}
	
	protected DeleteLink(InputBuffer buf) {
		try {
			this.from = (URL) XMLMarshaller.getInstance().unmarshall(buf.readUTF());
			this.to = (URL) XMLMarshaller.getInstance().unmarshall(buf.readUTF());
			this.user = buf.readUTF();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static DHTObjectDifference build(InputBuffer buf) {
		return new DeleteLink(buf);
	}
	
	@Override
	public void serialize(OutputBuffer buf) throws IOException {
		buf.writeShort(TYPE);
		buf.writeUTF(XMLMarshaller.getInstance().marshall(from));
		buf.writeUTF(XMLMarshaller.getInstance().marshall(to));
		buf.writeUTF(user);
	}

}
